package cn.openea.perpartner.componet.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 责任链处理器
 *
 * @author Weird077
 * @description TODO
 * @date 2023-03-06 21:50
 */
@Slf4j
@Component
public class ChainProcessor implements ChainHandler, ApplicationContextAware {
    private List<ChainHandler> handlerList;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Map<String, ChainHandler> serviceMap = applicationContext.getBeansOfType(ChainHandler.class);
        handlerList = new ArrayList<>(serviceMap.values());
        handlerList.remove(this);
    }
    @Override
    public boolean execute(Map<String, Object> params) {
        boolean result;
        for (ChainHandler handler : handlerList) {
            result = handler.execute(params);
            if (result) {
                break;
            }
        }
        return true;
    }
}
